<?php

namespace addons\TfFutures\api\controllers;

use addons\TfFutures\common\models\Order;
use addons\TfFutures\common\models\Lever;
use addons\TfFutures\common\models\Member;
use addons\TfFutures\common\models\MemberLever;
use Carbon\Carbon;
use common\helpers\ResultDataHelper;
use Yii;
use api\controllers\OnAuthController;

class LeverController extends OnAuthController
{
    public $modelClass = '';
    protected $optional = ['list'];
    //杠杆类型
    public function actionList(){
        if (!Yii::$app->request->isGet){
            return ResultDataHelper::api(403,'Access done！');
        }
        $model = Lever::find()->where(['status'=>'1'])->all();
        return ResultDataHelper::api(200,'ok',$model);
    }

    //申请配资
    public function actionAdd(){
        if (!Yii::$app->request->isPost){
            return ResultDataHelper::api(403,'Access done！');
        }
        $mylever = MemberLever::find()->where([
            'member_id'=>Yii::$app->user->identity->member_id,
            'state'    => '正常'
        ])->one();
        
        if( !empty($mylever) ){
            return ResultDataHelper::api(500,'杠杆已存在');
        }
        
        $lever = MemberLever::find()->where([
            'member_id'=>Yii::$app->user->identity->member_id,
            'state'    => '审核中'
        ])->one();
        
        if( !empty($lever) ){
            return ResultDataHelper::api(500,'已经存审核中的杠杆');
        }
        $model = new MemberLever();
        $model->attributes = Yii::$app->request->post();

        //根据id查询配资杠杆信息
        $leverModel         = Lever::findOne($model->lever_id);
        //用户资产信息
        $member_price = Member::find()->where(['member_id'=>Yii::$app->user->identity->member_id])->one();
        if( empty($leverModel) ){
            return ResultDataHelper::api(500,'杠杆合约不存在');
        }
        if($leverModel->min > $model->price){
        	return ResultDataHelper::api(500,'配资金额过低');
        }
        if($leverModel->max < $model->price){
        	return ResultDataHelper::api(500,'配资金额过高');
        }
        if($leverModel->limit > $member_price->price){
        	return ResultDataHelper::api(500,'余额不符合申请要求');
        }
        
        $model->price_lever = $model->price;
        $model->price       = bcmul($model->price,$leverModel->ratio,4);
        $model->sdate       = date('Y-m-d');
        $model->edate       = Carbon::parse($model->sdate)->addDays($leverModel->cycle)->format('Y-m-d');

        $model->member_id = Yii::$app->user->identity->member_id;

        if (!$model->save()) {
            return ResultDataHelper::api(500,$model->getFirstErrors());
        }
        return ResultDataHelper::api(200,'提交成功');
    }
    /*
     * 撤销配资
    */
	public function actionUndo(){
		 if (!Yii::$app->request->isPost){
            return ResultDataHelper::api(403,'Access done！');
        }
        $lever = Yii::$app->request->post('lever_id');
        if(empty($lever)){
        	return ResultDataHelper::api(422,'杠杆id不能为空');
        }
        $lever = MemberLever::find()->where([
            'member_id'=>Yii::$app->user->identity->member_id,
            'state'    => '正常'
        ])->one();
        if(empty($lever)){
        	return ResultDataHelper::api(422,'杠杆合约不存在');
        }
        $mylever = order::find()->where([
            'lever_id'=>$lever,
            'state'   => '持仓'
        ])->all();
        if(!empty($mylever)){
        	return ResultDataHelper::api(422,'当前配资有持仓订单');
        }
       $lever ->state = '禁用';
        if (!$lever->save()) {
            return ResultDataHelper::api(500,$lever->getFirstErrors());
        }
        return ResultDataHelper::api(200,'撤销成功');
	}

}